package cn.exrick.xboot.modules.Information.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Information.entity.CrossCheck;
import cn.exrick.xboot.modules.Information.entity.River;
import cn.exrick.xboot.modules.Information.entity.RiversSetUp;
import cn.exrick.xboot.modules.Information.service.ICrossCheckService;
import cn.exrick.xboot.modules.Information.service.IriverService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Lyg
 */
@Slf4j
@RestController
@Api(description = "断面考核管理接口")
@RequestMapping("/xboot/crossCheck")
@Transactional
public class CrossCheckController {

    @Autowired
    private ICrossCheckService iCrossCheckService;

    @Autowired
    private IriverService iriverService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<CrossCheck> get(@PathVariable Long id){

        CrossCheck crossCheck = iCrossCheckService.getById(id);
        return new ResultUtil<CrossCheck>().setData(crossCheck);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<CrossCheck>> getAll(){

        List<CrossCheck> list = iCrossCheckService.list();
        return new ResultUtil<List<CrossCheck>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<CrossCheck>> getByPage(@ModelAttribute PageVo page,@RequestParam Map map){
        QueryWrapper queren = new QueryWrapper();
        queren.eq("with_department",map.get("withDepartment"));
        queren.apply("date_format(create_time,'%Y-%m') = '"+map.get("startDate")+"'");
        IPage<CrossCheck> data = iCrossCheckService.page(PageUtil.initMpPage(page),queren);
        //河流名称赋值
        data.getRecords().forEach(item->{
            //按河流id查询河流名称
            River river= iriverService.getById(item.getRiverId());
            if(StringUtils.isNotEmpty(river.getRiverName())){
                item.setRiverName(river.getRiverName());
            }
        });

        return new ResultUtil<IPage<CrossCheck>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<CrossCheck> saveOrUpdate(@RequestParam String crossCheck){
        Gson gson = new Gson();
        List<CrossCheck> cr = gson.fromJson(crossCheck, new TypeToken<List<CrossCheck>>() {}.getType());
        try {
            cr.forEach(cross->{
                if(StringUtils.isNotEmpty(cross.getQualified())){
                    cross.setSta("2");
                }else{
                    cross.setSta("1");
                }
                iCrossCheckService.saveOrUpdate(cross);
            });
            return new ResultUtil<CrossCheck>().setData(new CrossCheck());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<CrossCheck>().setErrorMsg("操作失败");
        }
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iCrossCheckService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
